import { request } from 'umi'

import homeApi from './homeApi'

export const apiFn = <T = normalResProps>(apiParameters: apiParametersProps) => {
  const { method, opts = {}, urlPrefix = '/api', loading } = apiParameters
  let { url } = apiParameters
  const { urlData, urlPrefix: potsUrlPrefix } = opts
  opts.loading = opts.loading || loading

  // 默认的data
  if (['POST', 'PUT'].includes(method) && !opts.data) opts.data = {}

  // 处理urlData
  if (urlData) urlData.forEach((item: number | string) => (url = `${url}/${item}`))

  return request<T>(potsUrlPrefix ? potsUrlPrefix + url : urlPrefix + url, {
    ...opts,
    // skipErrorHandler: true, // 跳过umi的错误拦截
    method,
  })
}

export const API = {
  ...homeApi,
}
